﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Visitor;
using Taste;

namespace ASTClass
{
    class BlockAST : AST
    {
        public BlockAST() : base() { name = "Block"; }
        public BlockAST(AST parent) : base(parent) { name = "Block"; }

        public override AST Clone()
        {
            BlockAST ast = new BlockAST();
            ast.operators = operators;
            foreach (AST c in children)
                ast.children.Add(c.Clone());

            return ast;
        }

        public override void CodeGen(Taste.SymbolTable st, Taste.CodeFileGen cfg)
        {
            st.OpenScope(this.UniqueName);
            int i = 0;
            foreach (AST child in children)
            {
                if (child.Name == "VarDecl")
                {
                    foreach (AST grandchild in child.Children)
                    {
                        if (grandchild.Name == "Ident")
                        {

                            Var x = new Var(grandchild as IdentAST);
                            st.insertLocalVar(x);
                            i++;
                        }
                    }
                }
            }
            foreach (AST ast in children)
            {
                ast.CodeGen(st, cfg);
            }
            st.CloseScope();
        }

        #region Visitor pattern

        public override void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }

        #endregion Visitor pattern
    }
}
